package servlets;

import com.fasterxml.jackson.databind.ObjectMapper;
import dao.impl.CaseHistoryDaoImpl;
import entity.CaseHistory;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;


@WebServlet(name = "病历异步获取服务", value = "/fillCaseHistoryByPatientIdServlet")
public class FillCaseHistoryByPatientIdServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        request.setCharacterEncoding("UTF-8");
        String Patient_id = request.getParameter("Patient_id");
        String casehistoryNum = request.getParameter("casehistory_num");
        String json = "";
        List list1 = new ArrayList();
        ObjectMapper mapper = new ObjectMapper();
        CaseHistoryDaoImpl caseHistoryDao = new CaseHistoryDaoImpl();
        if (casehistoryNum == null) {
            List<CaseHistory> list = caseHistoryDao.getByPatientIdOrderByDate(Patient_id);
            mapper = new ObjectMapper();
            json = mapper.writeValueAsString(list);
        } else if (Patient_id == null) {

            CaseHistory caseHistory = caseHistoryDao.getByCaseHistoryId(casehistoryNum).get(0);
            mapper = new ObjectMapper();
            json = mapper.writeValueAsString(caseHistory);
        }


        response.setContentType("text/html;charset=UTF-8");
        mapper.writeValue(response.getWriter(), json);


    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        this.doPost(request, response);
    }
}